import { Request, Response, NextFunction } from 'express'
import authService from './auth.service';
const createError = require('http-errors');
class authController {
  static register = async (req: Request, res: Response, next: NextFunction) => {
    try {
      const user = await authService.register(req.body);
      console.log('user==', user);
      res.status(200).json({
        status: true,
        message: 'User created successfully',
        data: user
      })
    }
    catch (e: any ) {
      next(createError(e.statusCode, e.message))
    }
  }
  static login = async (req: Request, res: Response, next: NextFunction) => {
    try {
      const data = await authService.login(req.body);
      res.status(200).json({
        status: true,
        message: "Account login successful",
        data
      })
    } catch (err: any) {
      // next(createError(err.statusCode, err.message));
      console.log('e.statusCode==', err.statusCode);
      console.log('e.statusCode==', err.message);
      // @ts-ignoreNotFoundError
       // res.json(err.statusCode, { error: err.message });
      res.status(err.statusCode).json({
        status: false,
        message: err.message ,
        data: null
      })
    }
  }
  static all = async (req: Request, res: Response, next: NextFunction) => {
    try {
      const users = await authService.all();
      res.status(200).json({
        status: true,
        message: 'All users',
        data: users
      })
    }
    catch (err: any) {
      res.status(err.statusCode).json({
        status: false,
        message: err.message ,
        data: null
      })
    }
  }
}
export default authController;
